﻿using System;
using System.Linq;
using System.Collections.Generic;
using VideoSeller.Business.Data.Enums;
using VideoSeller.Business.Models;
using VideoSeller.Core.Configuration;
namespace VideoSeller.Business.Data
{
	public partial class OnlineContent
	{
		public OnlineContentType OnlineContentType
		{
			get
			{
				if (!Active && string.IsNullOrEmpty(DownloadUrl))
				{
					return OnlineContentType.Announcement;
				}

				if (Active)
				{
					return OnlineContentType.Online;
				}

				return OnlineContentType.History;
			}
		}

		public VideoContentType VideoContentType
		{
			get { return (VideoContentType) VideoContentTypeValue; }
		}

        public int Claims
        {
            get { return OnlineContentClaims.Count; }
        }

        public int Likes
        {
            get { return Math.Abs(OnlineRatings.Sum(x => x.Like)); }
        }

        public int UnLikes
        {
            get { return Math.Abs(OnlineRatings.Sum(x => x.Unlike)); }
        }

        public bool isWaitingStart
        {
            get { return !Active && string.IsNullOrEmpty(DownloadUrl) && BroadcastStart.HasValue;  }
        }

        public bool isPaid
        {
            get { return MinimalPrice.HasValue && MinimalPrice.Value > 0; }
        }

		public bool AlreadyRate(Guid userId)
		{
			return OnlineRatings.Any(x => x.UserId == userId);
		}

        public string CultureImage
        { 
            get {
            
            String culturename = Culture == null ? "" : Culture;
            if (culturename != "")
                return string.Format("~/content/images/lang_{0}_small.png", culturename);
            else
                return "";

            }
        }
        public string CultureCountry
        {
            get
            {
                if (String.IsNullOrEmpty(Culture))
                    return "";
                var resourceModel = IoC.Resolve<ResourceModel>();
                System.Globalization.CultureInfo _culture = resourceModel.GetUniqueCultureInfos().Where(x=>x.Name == Culture).FirstOrDefault();
                String[] nn = _culture.NativeName.Split(new char [] { ' ' },StringSplitOptions.RemoveEmptyEntries);
                if (nn.Length > 0)
                    return nn[0];
                else
                    return _culture.NativeName;

            }
        }
        public decimal Price
        {
            get
            {
                return MinimalPrice.HasValue ?  MinimalPrice.Value : 0;
             }
        }
        public string lenHistory
        {
            get{
          TimeSpan ts = TimeSpan.FromMilliseconds(OnlineContentHistories.Sum(m => m.Lenght == null ? 0 : m.Lenght.Value));
            return ts.Hours.ToString() + ":" + ts.Minutes.ToString() +":"+ ts.Seconds.ToString();
    }
        }
        public int WaitCount
        {
            get { return Waits.Count(); }
        }
        public int Views_online { get {return ViewCnt == null ? 0 : ViewCnt.Value; } }
        public int View_current { get { return OnlineContentPlays.Count(); } }
        public int Views_anonce { get {return ViewCntA == null ? 0 : ViewCntA.Value; } }
        public int Views_history { get { return ViewCntH == null ? 0 : ViewCntH.Value; } }
        public DateTime broadcastStart {get {return BroadcastStart.HasValue ? BroadcastStart.Value : DateTime.MinValue;}}
		public DateTime broadcastEnd {get {return BroadcastEnd.HasValue ? BroadcastEnd.Value : DateTime.MinValue;}}
		public bool IsWaitingAnnouncement {get {return DateTime.Now > BroadcastStart;}}
        public decimal CollectedMoney { get { return Paids.Sum(x => x.Value); } }
        public decimal CollectedMoneyExt { get { return Paids.Sum(x => x.EValue.HasValue ? x.EValue.Value : 0); } }
    }

}